package org.ceteca.explica.core.partners;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.CurrencyOT;
import org.ceteca.explica.core.common.PriceListOT;









/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class PartnerOT extends BaseVO implements Serializable {

	

	private static final long serialVersionUID = 1L;

	private long idPartner;

	private String name;

	private String brandName;

	private String idNumber;

	private String codeEDI;
	private String mainContact;
	private String phone1;
	private String phone2;
	private String fax;
	private String email;
	private Integer version = new Integer(0);
	

	

	private Integer vatRegime;

	private Integer numCopies;

	private BigDecimal maxRisk;

	private BigDecimal currentRisk;
	
	private Double promptPaymentDiscount, fixedDiscount;
	

	/**
	 * Relations
	 */

	private PartnerGroupOT partnerGroup;
	private RappelTypeOT rappelType;
	// private InvoicingSerieOT invoicingSerie;
	private CurrencyOT currency;
	
	private PaymentMethodOT paymentMethod;

	private Set<AddressOT> addressSet;

	private Set<BankAccountOT> bankAccountSet;

	private VendorOT vendor;

	private Set<ContactOT> contactSet;

	private PriceListOT priceList;
	
	
	
	











	public Integer getNumCopies() {
		return numCopies;
	}

	public void setNumCopies(Integer numCopies) {
		this.numCopies = numCopies;
	}

	public BigDecimal getMaxRisk() {
		return maxRisk;
	}

	public void setMaxRisk(BigDecimal maxRisk) {
		this.maxRisk = maxRisk;
	}

	public BigDecimal getCurrentRisk() {
		return currentRisk;
	}

	public void setCurrentRisk(BigDecimal currentRisk) {
		this.currentRisk = currentRisk;
	}

	public Set<AddressOT> getAddressSet() {
		return addressSet;
	}

	public void setAddressSet(Set<AddressOT> addressSet) {
		this.addressSet = addressSet;
	}

	
	 public Set<BankAccountOT> getBankAccountSet() { return bankAccountSet; }
	 
	 public void setBankAccountSet(Set<BankAccountOT> bankAccountSet) {
	 this.bankAccountSet = bankAccountSet; }
	 
	public VendorOT getVendor() {
		return vendor;
	}

	public void setVendor(VendorOT agent) {
		this.vendor = agent;
	}

	/**
	 * 
	 */
	/*
	 * private Set<DiscountOT> disccounts;
	 */

	public PartnerGroupOT getPartnerGroup() {
		return partnerGroup;
	}

	public void setPartnerGroup(PartnerGroupOT partnerGroup) {
		this.partnerGroup = partnerGroup;
	}

	public RappelTypeOT getRappelType() {
		return rappelType;
	}

	public void setRappelType(RappelTypeOT rappelType) {
		this.rappelType = rappelType;
	}

	/*
	 * public InvoicingSerieOT getInvoicingSerie() { return invoicingSerie; }
	 * 
	 * public void setInvoicingSerie(InvoicingSerieOT invoicingSerie) {
	 * this.invoicingSerie = invoicingSerie; }
	 * 
	 * public CurrencyOT getCurrency() { return currency; }
	 * 
	 * public void setCurrency(CurrencyOT currency) { this.currency = currency;
	 * }
	 */
	  public PaymentMethodOT getPaymentMethod() { return paymentMethod; }
	 
	 public void setPaymentMethod(PaymentMethodOT paymentMethod) {
	 this.paymentMethod = paymentMethod; }
	 /* 
	 * public Set<DiscountOT> getDisccounts() { return disccounts; }
	 * 
	 * public void setDisccounts(Set<DiscountOT> disccounts) { this.disccounts
	 * = disccounts; }
	 */
	public PartnerOT() {
		super();
		this.addressSet=new HashSet<AddressOT>();
		this.bankAccountSet= new HashSet<BankAccountOT>();
		this.contactSet= new HashSet<ContactOT>();
		
	}

	public Integer getVersion() {
		return version;
	}



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getIdNumber() {
		return idNumber;
	}

	public void setIdNumber(String idNumber) {
		this.idNumber = idNumber;
	}

	public String getCodeEDI() {
		return codeEDI;
	}

	public void setCodeEDI(String codeEDI) {
		this.codeEDI = codeEDI;
	}

	public String getMainContact() {
		return mainContact;
	}

	public void setMainContact(String mainContact) {
		this.mainContact = mainContact;
	}

	public String getPhone1() {
		return phone1;
	}

	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


	@Override
	public boolean equals(Long id) {
		return id != null && this.idPartner == id.longValue();
	}

	@Override
	public String getEntityId() {
		return InterfacePartners.EXPLICA_ENTITY_PARTNER_ID;

	}

	@Override
	public long getInternalId() {

		return idPartner;
	}

	@Override
	public String getInternalValue() {

		return name;
	}

	@Override
	public String toString() {

		StringBuffer result = new StringBuffer();

		try {
			result.append("\n%%%%%%%%%%%% BEGIN PARTNER DETAIL %%%%%%%%%%%%%%");
			result.append("\nID: ");
			result.append(this.idPartner);
			result.append("\nName: ");
			result.append(this.name);
			result.append("\nBrand Name: ");
			result.append(this.brandName);
			/*
			if (getContactSet() != null) {
				
				result.append("\n************ CONTACTS SET *********************"+getContactSet().size());
				for (ContactOT c : getContactSet()) {
					result.append("\n" + c.toString());
				}
			}
			*/
			if (getAddressSet() != null) {
				
				result.append("\n************ ADDRESS SET *********************"+getAddressSet().size());
				for (AddressOT c : getAddressSet()) {
					result.append("\n" + c.toString());
				}
			}
			
			result.append("\n%%%%%%%%%%%% END PARTNER DETAIL%%%%%%%%%%%%%%%%");
		} catch (Exception ex) {
			System.out.println("Error formatting Partner!");
			ex.printStackTrace();
			return null;
		}

		return result.toString();

	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Set<ContactOT> getContactSet() {
		return contactSet;
	}

	public void setContactSet(Set<ContactOT> contactSet) {
		this.contactSet = contactSet;
	}

	public CurrencyOT getCurrency() {
		return currency;
	}

	public void setCurrency(CurrencyOT currency) {
		this.currency = currency;
	}

	
	
	
	public long getIdPartner() {
		return idPartner;
	}

	public void setIdPartner(long idPartner) {
		this.idPartner = idPartner;
	}

	

	public PriceListOT getPriceList() {
		return priceList;
	}

	public void setPriceList(PriceListOT priceList) {
		this.priceList = priceList;
	}

	
	
	   @Override
	    public int hashCode() {
	        final HashCodeBuilder hcb = new HashCodeBuilder();
	        hcb.append(getIdNumber());
	        return hcb.toHashCode();
	    }

	    
	    
	    @Override
	    public boolean equals(Object obj) {
	        boolean isEquals = false;
	        try {
	            final PartnerOT other = (PartnerOT)obj;
	            final EqualsBuilder eqb = new EqualsBuilder();
	            eqb.append(getIdNumber(), other.getIdNumber());
	            isEquals = eqb.isEquals();

	        } catch (Exception e) {
	            isEquals = false;
	        }

	        return isEquals;
	    }

	    
	    public String getPartnerGroupName(){
	    	if (getPartnerGroup()==null)
	    		return "";
	    	else
	    		return getPartnerGroup().getDescription();
	    }

		public Integer getVatRegime() {
			return vatRegime;
		}

		public void setVatRegime(Integer vatRegime) {
			this.vatRegime = vatRegime;
		}

		public Double getPromptPaymentDiscount() {
			return promptPaymentDiscount;
		}

		public void setPromptPaymentDiscount(Double promptPaymentDiscount) {
			this.promptPaymentDiscount = promptPaymentDiscount;
		}

		public Double getFixedDiscount() {
			return fixedDiscount;
		}

		public void setFixedDiscount(Double fixedDiscount) {
			this.fixedDiscount = fixedDiscount;
		}
	    
		
		public ArrayList<AddressOT> getBillingAddress(){
			ArrayList<AddressOT> result= new ArrayList<AddressOT>();
			for (AddressOT  i: addressSet)
				if (i.isBillingAddress())
					result.add(i);
			return result;
		}
	
		public ArrayList<AddressOT> getShippingAddress(){
			ArrayList<AddressOT> result= new ArrayList<AddressOT>();
			for (AddressOT  i: addressSet)
				if (i.isShippingAddress())
					result.add(i);
			return result;
		}
	    
	    
}
